package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestPermission;
import com.kgc.scd.annotation.RequestUser;
import com.kgc.scd.bean.Seckill;
import com.kgc.scd.service.SecKillService;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.UserAnno;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: com.kgc.scd.controller
 * @author: HarryXzy
 * @create: 2022-06-15 14:04
 * @description:
 **/
@RestController
@Api(tags = "05、秒杀通知操作入口")
public class SecKillController {

    @Autowired
    private SecKillService secKillService;

    /**
     * @author: HarryXzy
     * @date: 2022/6/14 13:25
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @description: 获取所有可以参加的活动
     */
    @ApiOperation(value = "当前秒杀活动", notes = "获取当前所有可以参加的活动")
    // 返回状态说明
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 200, message = "获取成功")
    })
    @GetMapping("/getAllSecKill")
    public RequestResult<Object> getAllSecKill(){
        Map<String, Object> result = secKillService.getAllSecKill();
            if (result.get("code").equals("200")) {
                return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code").toString(), result.get("msg").toString());
    }


    /**
     * @author: HarryXzy
     * @date: 2022/6/15 09:32
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @description: 秒杀登记
     */
    @ApiOperation(value = "秒杀登记", notes = "秒杀登记，通知秒杀活动")
    // 返回状态说明
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 501, message = "已经选择通知秒杀活动"),
            @ApiResponse(code = 503, message = "秒杀活动已结束"),
            @ApiResponse(code = 502, message = "秒杀活动即将开始"),
            @ApiResponse(code = 4444, message = "操作过于频繁"),
            @ApiResponse(code = 200, message = "订阅成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "secKillid", value = "秒杀活动ID")
    })
    @RequestPermission
    @GetMapping("/needNotifySecKill")
    public RequestResult<Object> needNotifySecKill(@RequestUser UserAnno userAnno,
                                                 @RequestParam("secKillid") String secKillid){
        Map<String, Object> result = secKillService.needNotifySecKill(userAnno.getUserId(),secKillid);
        if (result.get("code").equals("200")) {
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code").toString(), result.get("msg").toString());
    }



    /**
     * @author: HarryXzy
     * @date: 2022/6/15 09:32
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @description: 添加秒杀活动
     */
    @ApiOperation(value = "添加秒杀活动", notes = "添加秒杀活动")
    // 返回状态说明
    @ApiResponses({
            @ApiResponse(code = 500, message = "系统错误"),
            @ApiResponse(code = 503, message = "开始或结束时间有错误"),
            @ApiResponse(code = 502, message = "秒杀商品不存在"),
            @ApiResponse(code = 200, message = "添加成功")
    })
    @RequestPermission
    @PostMapping("/addSecKill")
    public RequestResult<Object> addSecKill(@RequestBody() Seckill seckill){
        Map<String, Object> result = secKillService.addSecKill(seckill);
        if (result.get("code").equals("200")) {
            return ResultBuildUtil.success(result.get("data"));
        }
        return ResultBuildUtil.fail(result.get("code").toString(), result.get("msg").toString());
    }

}
